package net.hn.hnms.biz.performance.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.performance.domain.PerformanceMetrics;
import net.hn.hnms.biz.performance.mapper.PerformanceMetricsMapper;
import net.hn.hnms.biz.performance.request.PerformanceMetricsReq;
import net.hn.hnms.biz.performance.service.PerformanceMetricsService;
import net.hn.hnms.biz.performance.vo.PerformanceMetricsVo;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Component
public class PerformanceMetricsServiceImpl extends BaseService<PerformanceMetrics, PerformanceMetricsVo> implements PerformanceMetricsService {

    @Autowired
    private PerformanceMetricsMapper performanceMetricsMapper;


    @Override
    public ResponsePage<PerformanceMetricsVo> selectPageList(PerformanceMetricsReq dimDrivageFace, PageQuery pageQuery) {
        Page<PerformanceMetrics> page = performanceMetricsMapper.selectPage(pageQuery.build(), buildQuery(dimDrivageFace).orderByDesc(PerformanceMetrics::getCreateTime));
        List<PerformanceMetricsVo> list = page.getRecords().stream().map(o -> convert(o, PerformanceMetricsVo.class)).collect(Collectors.toList());
        return new ResponsePage<>(list, page.getTotal());
    }

    @Override
    public PerformanceMetrics selectPerformanceMetricsById(Integer performanceMetricsId) {

        PerformanceMetrics performanceMetrics = performanceMetricsMapper.selectById(performanceMetricsId);
        if (performanceMetrics == null) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        return performanceMetrics;
    }

    @Override
    public int insert(PerformanceMetricsReq dimDrivageFace) {
        List<PerformanceMetrics> performanceMetrics = performanceMetricsMapper.selectList(buildQueryRepeat(dimDrivageFace));
        if (performanceMetrics .size()>0) {
            throw new HuaNengException(Constants.FAIL, "指标重复");
        }
        PerformanceMetrics insertObj = new PerformanceMetrics();
        BeanUtil.copyProperties(dimDrivageFace, insertObj);
        return performanceMetricsMapper.insert(insertObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(List<String> idList) {
        performanceMetricsMapper.deleteBatchIds(idList);
    }

    @Override
    public void updateDeleteByIds(List<Integer> idList) {
        performanceMetricsMapper.updateDeleteByIds(idList);
    }

    @Override
    public void updateById(PerformanceMetricsReq dimDrivageFace) {
        List<PerformanceMetrics> performanceMetrics = performanceMetricsMapper.selectList(buildQueryRepeat(dimDrivageFace));
//        if (performanceMetrics .size()>0) {
//            throw new HuaNengException(Constants.FAIL, "指标重复");
//        }
        PerformanceMetrics updateObj = new PerformanceMetrics();
        BeanUtil.copyProperties(dimDrivageFace, updateObj);
        int updateCount = performanceMetricsMapper.updateById(updateObj);
        if (updateCount == 0) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
    }


    /**
     * 构造查询条件
     * @param req
     * @return LambdaQueryWrapper<DimDrivageFace> 查询条件
     */
    private LambdaQueryWrapper<PerformanceMetrics> buildQuery(PerformanceMetricsReq req){
        return new LambdaQueryWrapper<PerformanceMetrics>()
                .like(StrUtil.isNotBlank(req.getPerformanceMetricsName()), PerformanceMetrics::getPerformanceMetricsName, req.getPerformanceMetricsName())
                .in(req.getMetricsIds()!=null&&req.getMetricsIds().length>0, PerformanceMetrics::getPerformanceMetricsId, req.getMetricsIds());
    }

    private LambdaQueryWrapper<PerformanceMetrics> buildQueryRepeat(PerformanceMetricsReq req){
        return new LambdaQueryWrapper<PerformanceMetrics>()
                .eq(StrUtil.isNotBlank(req.getPerformanceMetricsName()), PerformanceMetrics::getPerformanceMetricsName, req.getPerformanceMetricsName());

    }
}
